package work.stackandqueque;

import java.util.Stack;

public class Problem1047 {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        String ans = "";
        for (int i = 1; i < s.length(); i++) {
            if (stack.empty()) stack.push(s.charAt(i));
            else if (s.charAt(i) == stack.peek()) stack.pop();
            else stack.push(s.charAt(i));
        }
        while (!stack.empty()) {
            ans = stack.pop() + ans;
        }
        return ans;
    }

    public static void main(String[] args) {
        Problem1047 problem1047 = new Problem1047();
        problem1047.removeDuplicates("abbaca");
    }
}
